package de.helen.ball;

import java.text.DecimalFormat;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;

public class Holzding extends SurfaceView implements Runnable, SurfaceHolder.Callback{

	int x = 30;	
	
	
	   // Surface holder allows to control and monitor the surface
    private SurfaceHolder mHolder;
 
    // A thread where the painting activities are taking place
    private Thread mThread;
 
    // A flag which controls the start and stop of the repainting of the SurfaceView
    private boolean mFlag = false;
 
    // X coordinate of the touched position
    private float mX;
 
    // Y Coordinate of the touched position
    private float mY;
 
    // Paint
    private Paint mPaint;
    private Paint mPaintHole;
 
    private Physik phy;
    
    public Holzding(Context context, AttributeSet attrs) {
        super(context, attrs);
 
 
    }
    
    
    
    
 
    public void resume(){

    	// Set dummy values
	    phy = new Physik(50, 50, 10, 10);
	    
	     
	    
        mHolder = getHolder();
        mHolder.addCallback(this);
        
        // Initializing the X position
        mX = -100;
 
        // Initializing the Y position
        mY = -100;
 
        // Initializing the paint object mPaint
        mPaint = new Paint();
        mPaintHole = new Paint();
 
        // Setting the color for the paint object
        mPaint.setColor(Color.BLUE);
        paintWhite.setTextSize(30);
        mPaintHole.setColor(Color.YELLOW);
        paintBlack.setColor(Color.BLACK);
        paintWhite.setColor(Color.WHITE);
        paintRed.setColor(Color.RED);
        
        // Instantiating the thread
        mThread = new Thread(this);
 
        // setting the mFlag to true for start repainting
        mFlag = true;
 
        // Start repaint the SurfaceView
        mThread.start();
    }
 
    public void pause(){
        mFlag = false;
    }
 
    public boolean onTouch(View v, MotionEvent event) {
        switch(event.getAction()){
        case MotionEvent.ACTION_DOWN:
            // Getting the X-Coordinate of the touched position
            mX = event.getX();
 
            // Getting the Y-Coordinate of the touched position
            mY = event.getY();
            break;
        }
        return true;
    }

    Paint paintBlack = new Paint();
    Paint paintWhite = new Paint();
    Paint paintRed = new Paint();
    
    long lastTime = 0;
    long timeInHole = 0;

    // mindestens 1 Vorkommastelle, genau 2 Nachkommastellen
	DecimalFormat f = new DecimalFormat("#0.000"); 
	int punkte = 0;
	
	
    @Override
    public void run() {
        while(mFlag){
            // Check whether the object holds a valid surface
            if(!mHolder.getSurface().isValid())
                continue;
            // Start editing the surface
            Canvas canvas = mHolder.lockCanvas();

    	    canvas.drawColor(Color.BLACK);
    	 
    	    // x,y berechnen
    	    
    	    phy.Update();

    	    canvas.drawText("Punkte: " + punkte, 100, canvas.getHeight() - 150, paintWhite);
    	    
    	    if (phy.IsInHole()){
    	    	timeInHole += System.currentTimeMillis() - lastTime;
    	    	canvas.drawCircle(200, 200, 40, paintWhite);
    	    	canvas.drawCircle(200, 200, 35, paintBlack);
    	    	
    	    	canvas.drawText("Erreichte Zeit: " + f.format(timeInHole / 1000.0) + " s", 100, canvas.getHeight() - 100, paintWhite);
    	    	if (timeInHole > 3000){
    	    		punkte++;
    	    		phy.Reset(300, 400);
    	    	}
    	    	
    	    }else{
    	    	timeInHole = 0;
    	    }
    	    
    	    // Loch
    	    canvas.drawCircle(200, 200, 30, mPaint);
    	    
    	    // Ball
    	    canvas.drawCircle((int)phy.GetX(), (int)phy.GetY(), 10, paintRed);
    	    
    	    lastTime = System.currentTimeMillis();
    	        	        	    
            // Finish editing the canvas and show to the user
            mHolder.unlockCanvasAndPost(canvas);
        }
    }


	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
		phy.ChangeSize(width, height);
	}





	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		
	}





	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		// TODO Auto-generated method stub
		
	}
}
	
	
